﻿using MiniExcelLibs;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.超级管理员
{
    public partial class Form盘点计划 : UIPage
    {
        public Form盘点计划()
        {
            InitializeComponent();
        }

        private void Form盘点计划_Load(object sender, EventArgs e)
        {
            DataTable dt = 查询盘点计划();
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.Refresh();
        }

        private DataTable 查询盘点计划()
        {
            string sql = "select guid,CheckPlanCode 盘点计划编号,CheckPlanName 盘点计划名称,Creator 创建人,CreateDate 创建日期 from Bus_AllCheckPlan order by CreateDate desc";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            return dt;
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //查盘点结果
            string guid = uiDataGridView1.Rows[e.RowIndex].Cells["Cguid"].Value.ToString();
            DataTable dt = 查盘点结果(guid);
            uiDataGridView2.DataSource = dt;
            uiDataGridView2.Refresh();
            //如果是导出
            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "C导出")
            {
                DataTable dts = 查盘点结果(guid);
                string path;
                this.saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
                if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.saveFileDialog1.FileName;
                    if (!File.Exists(path))
                    {
                        MiniExcel.SaveAs(path, dts, true, "sheet1", ExcelType.XLSX);
                    }
                    else
                    {
                        MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                        MessageBox.Show("导出成功");
                        return;
                    }
                }
            }
        }
        private DataTable 查盘点结果(string guid)
        {
            string sql = $@"SELECT guid, bguid, CheckPlanCode, 工厂编号, 生产批号, 存货编号, 品名, 钢号, 条码, 长度, 规格, 等级, 表面状态, 
                   表面处理, 净重, 备注, 生产日期, 检验员, 操作ip, 操作人员, 操作时间, 是否复盘, 库位, 仓库
                    FROM      Bus_AllCheckResult where guid = '{guid}'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            return dt;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(uiTextBox计划名称.Text.Trim()) || string.IsNullOrWhiteSpace(uiTextBox计划编号.Text.Trim()))
            {
                MessageBox.Show("请输入计划编号和计划名称");
            }
            else
            {
                //更新以前的计划状态
                string sql = "update Bus_AllCheckPlan set CheckPlanStatus = 0";
                pub.ExecuteSql(pub.ConnectionString, sql);
                //塞入新计划
                string sql2 = $@"insert into Bus_AllCheckPlan (guid, WorkShopCode, CheckPlanCode, CheckPlanName, CheckPlanStatus, CreateID, Creator, CreateDate)
                                                       values (newid(),'3001','{uiTextBox计划编号.Text.Trim()}','{uiTextBox计划名称.Text.Trim()}','1','{pub.sys_user.User_Id}','{pub.sys_user.UserName}',getdate())";

                int r = pub.ExecuteSql(pub.ConnectionString, sql2);
                if (r <= 0)
                {
                    MessageBox.Show("添加失败，请联系开发者");
                }
                else
                {
                    MessageBox.Show("添加成功");
                    DataTable dt = 查询盘点计划();
                    uiDataGridView1.DataSource = dt;
                    uiDataGridView1.Refresh();
                }
            }
        }
        private void uiButton查询_Click(object sender, EventArgs e)
        {
            DataTable dt = 查询盘点计划();
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.Refresh();
        }
    }

       
    }
